<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');
class OrderController extends Controller {
	public function index(){
		 //dump($_POST);

		if($_POST['test']==0){
			$this->error('请选择您要购买的商品',''.__MODULE__.'/cart/index/goodsNum/1');
		}
		
		//接受post传参过来的值
		//要购买的商品id
		$goodsids=I('post.goodsid');

		$ids=implode(',',$goodsids);

		//当前用户id
		$userid=I('post.userid');
		//订单总价
		$total=I('post.test');
		$totalall=$total + 10;
		//分配到模板
		$this->assign('ids',$ids);
		$this->assign('total',$total);
		$this->assign('totalall',$totalall);

		$ordernumber=$_SESSION['id'].time();
		$this->assign('ordernumber',$ordernumber);
		$addtime=date('Y-m-d H:i:s',time());
		$this->assign('addtime',$addtime);

		//实例化cart表
		$cart=M('Cart');
		$map['goodsid']=array('in',$ids);
		$map['userid']=$userid;
		$mycart=$cart->where($map)->select();

		$this->assign('mycart',$mycart);

		$this->display();

    }

    public function orderdetail(){
    	$order=M('Orders');
    	$detail=M('Detail');
    	$order_number=I('order_number');

    	$myorder=$order->join('yg_detail ON yg_orders.order_number = yg_detail.order_number')
    	               ->where('yg_detail.order_number='.$order_number)
    	               ->select();

        if($myorder['0']['state']=="已收货"){
            $ca="评价";
        }else{
            $ca="";
        }
        $this->assign('ca',$ca);
	    $this->assign('myorder',$myorder);
    	$this->display();
    }

    public function myorder(){
    	$userid=$_SESSION['id'];
    	$ord=M('orders');
    	$myorder=$ord->where('user_id='.$userid)->order('id desc')->select();

    	$arr=array();
    	foreach ($myorder as $k => $v) {
    		$arr[]='<li>
            <table class="order-list j_product">
                <thead>
                    <tr>
                        <th colspan="4"><div><span>订单号:'.$v['order_number'].'</span><span>提交时间:'.$v['addtime'].'</span></div></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td width="520" class="all-goods">';
                        $detail=M('Detail');
                    	$img=$detail->where('order_number='.$v['order_number'])->select();

                            foreach ($img as $key => $value) {

                            	$arr[]='<a><img class="j_product_img" src="'.__ROOT__.'/Public/Upload/'.$value['goodspic'].'"></a>';
                            }
                        $arr[]='</td>
                        <td class="amount" width="140">￥'.$v['totalall'].'</td>
                        <td width="140">'.$v['state'].'</td>
                        <td width="140" class="operate">';
                        	if($v['state']=='取消订单' || $v['state']=='已收货'){
                        		$cancel='none';
                        	}else{
                        		$cancel='block';
                        	}

                        	if($v['state']=='取消订单' || $v['state']=='已收货'){
                        		$sure='none';
                        	}else{
                        		$sure='block';
                        	}

                        	if($v['state']=='已收货'){
                        		$comment='block';
                        	}else{
                        		$comment='none';
                        	}

							if($v['state']=='已支付' || $v['state']=='已收货' ||$v['state']=='取消订单'){
								$pay='none';
							}else{
								$pay='block';
								$sure='none';
							}

                            $arr[]='<div style="display:block">
                                <a target="_self" href="'.__CONTROLLER__.'/orderdetail/order_number/'.$v['order_number'].'">查看详情</a>
                            </div>
                            <div style="display:'.$cancel.'">
                                <a target="_self" href="'.__CONTROLLER__.'/off/order_number/'.$v['order_number'].'">取消订单</a>
                            </div>
                            <div style="display:'.$sure.'">
                                <a target="_self" href="'.__CONTROLLER__.'/sure/order_number/'.$v['order_number'].'">确认收货</a>
                            </div>
                            <div style="display:'.$comment.'">
                                <a target="_self" href="'.__MODULE__.'/Comment/index/order_number/'.$v['order_number'].'">去评价</a>
                            </div>
                            <div style="display:'.$pay.'">
                                <a target="_self" href="'.__MODULE__.'/Orderover/index/order_number/'.$v['order_number'].'">去支付</a>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </li>';
    	}
    	


    	$this->assign('arr',$arr);
    	$this->display();
    }

     public function off(){
        $order_number=I('get.order_number');

       $order=M('orders');

       $order->create();
       $orders=$order->where('order_number='.$order_number)->data('state=取消订单')->save();

       if($orders){
            $this->success('订单取消成功',''.__CONTROLLER__.'/myorder');
        }else{
            $this->error('订单取消失败',''.__CONTROLLER__.'/myorder');
        }
    }

    public function sure(){
       $order_number=I('get.order_number');

       $order=M('orders');

       $order->create();
       $orders=$order->where('order_number='.$order_number)->data('state=已收货')->save();

       if($orders){
            $this->success('收货成功',''.__CONTROLLER__.'/myorder');
        }else{
            $this->error('收货失败',''.__CONTROLLER__.'/myorder');
        }
    }
		
}
